﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Openfree.Framework.DataAccess;

public partial class Controls_Activity_GoodStockIn : BaseUserControl
{  
    private SqlCodeConfig codeConfig;

    public string m_Table = "PMS_TaskItem";

    string sql = @"select a.*,C.Guest,C.Phone,C.Location,C.CreateDate
                    ,(select CodeName from T_CodeConfig where CodeValue=a.SNGroup and Category='" + PMSCodeCategory.PMS_BMZB + @"') as GroupName
                    ,(select CodeName from T_CodeConfig where CodeValue=a.SNTask and Category='" + PMSCodeCategory.PMS_TASKTYPE + @"') as TaskName 
                        from PMS_TaskItem a left join PMS_TaskLX C on c.TaskID = a.TaskID where C.IsRequestGood=1";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillSelectGirdView(0, sql);
            InitData();
        }
        
    }
    private void InitData()
    {
        codeConfig = new SqlCodeConfig(this.Connection);
        DataSet ds = codeConfig.LoadDataByCategory(PMSCodeCategory.PMS_BMZB);
        if (ds != null && ds.Tables.Count > 0)
        {
            tbGroup.DataSource = ds.Tables[0];
            tbGroup.DataTextField = "CodeName";
            tbGroup.DataValueField = "CodeValue";
            tbGroup.DataBind();
        }
        DataSet taskds = codeConfig.LoadDataByCategory(PMSCodeCategory.PMS_TASKTYPE);
        if (taskds != null && taskds.Tables.Count > 0)
        {
            S_SNTask.DataSource = taskds.Tables[0];
            S_SNTask.DataTextField = "CodeName";
            S_SNTask.DataValueField = "CodeValue";
            S_SNTask.DataBind();
        }
    } 
    protected void gridViewSelect_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
        }
    }

    protected void gridViewSelect_SplitPageIndexChange(object sender, CommandEventArgs e)
    {
        FillSelectGirdView(Int32.Parse(e.CommandArgument.ToString()), ViewState[this.ClientID + "cmdText"].ToString());
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        tbGroup.SelectByValue = tbGroup.SelectedValue;
        string cmdText = sql + " where C.IsRequestGood=1 ";

        if (!string.IsNullOrEmpty(S_TaskCode.Text.Trim()))
        {
            cmdText += " and a.TaskCode like '%{0}%' ";
            cmdText = string.Format(cmdText, S_TaskCode.Text.Trim());
        }
        if (!string.IsNullOrEmpty(S_SNTask.SelectedValue))
        {
            cmdText += " and a.SNTask = '{0}' ";
            cmdText = string.Format(cmdText, S_SNTask.SelectedValue);
        }
        if (!string.IsNullOrEmpty(tbGroup.SelectedValue))
        {
            cmdText += " and a.SNGroup = '{0}' ";
            cmdText = string.Format(cmdText, tbGroup.SelectedValue);
        }
        if (cmdText.EndsWith("and "))
        {
            cmdText = cmdText.TrimEnd(new char[] { 'a', 'n', 'd', ' ' });
        }

        FillSelectGirdView(0, cmdText);
    }
    protected void FillSelectGirdView(int currentPage, string cmdText)
    {
        if (string.IsNullOrEmpty(cmdText))
        {
            cmdText = ViewState[this.ClientID + "cmdText"].ToString();
        }
        ViewState[this.ClientID + "cmdText"] = cmdText;
        SqlSplitPage splitPage = new SqlSplitPage(base.Connection, cmdText);
        gridViewSelect.PageSize = 10;
        DataSet ds = splitPage.LoadDataSet(currentPage);
        gridViewSelect.ShowFooter = true;
        gridViewSelect.NavigationData = splitPage.GetNavigationData(currentPage);
        gridViewSelect.DataSource = ds.Tables[0];
        gridViewSelect.DataBind();
    } 
}
